import java.util.Scanner;

class Node{
    int i,j,v;

    public Node() {

    }
    public Node(int i, int j, int v) {
        this.i = i;
        this.j = j;
        this.v = v;
    }
    public String toString() {
        return this.i+" "+this.j+" "+this.v;
    }
}

class tripletable{
    Node[] data;
    int m,n,t;
    public tripletable(int m,int n ) {
        data = new Node[100];
        this.m = m;
        this.n = n;
        this.t = 0;
    }
    public void print() {
        for(int i=0;i<this.t;i++) {
            System.out.println(this.data[i]);
        }
    }
    /* 请在这里填写答案 */
    void add(int i,int j,int v)
    {
        data[t++] = new Node(i,j,v);
    }
    tripletable trans()
    {
        tripletable T = new tripletable(n,m);
        if (t!=0) {
            for (int col=0;col<n; col++) {
                for (int p=0; p<t; p++) {
                    if (this.data[p].j==col) {
                        T.add(data[p].j,data[p].i,data[p].v);
                    }
                }
            }
        }
        return T;
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner (System.in);
        int m = sc.nextInt();
        int n = sc.nextInt();
        int t = sc.nextInt();

        tripletable a = new tripletable(m,n);

        for(int i=0;i<t;i++) {
            a.add(sc.nextInt(), sc.nextInt(), sc.nextInt());
        }
        System.out.println(a.data[0]);

        tripletable b = a.trans();
        b.print();
        sc.close();
    }
}